package cn.yzx.pethome.system.shop.service.impl;

import cn.yzx.pethome.constants.BaseConstants;
import cn.yzx.pethome.system.shop.domain.Shop;
import cn.yzx.pethome.system.shop.domain.ShopEx;
import cn.yzx.pethome.system.shop.domain.ShopRo;
import cn.yzx.pethome.system.shop.mapper.ShopMapper;
import cn.yzx.pethome.system.shop.query.ShopQuery;
import cn.yzx.pethome.system.shop.service.IShopService;
import cn.yzx.pethome.base.service.impl.BaseServiceImpl;
import cn.yzx.pethome.system.user.domain.Employee;
import cn.yzx.pethome.system.user.mapper.EmployeeMapper;
import cn.yzx.pethome.utils.AuthService;
import cn.yzx.pethome.utils.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yanzixuan
 * @since 2023-01-07
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private AuthService authService;

    @Override
    public void add(Shop shop) {
        //添加判断
        if(shop.getName() == null){
            throw new BusinessException("店铺名不能为空");
        }
        if(shop.getTel() == null){
            throw new BusinessException("联系电话不能为空");
        }
        if(shop.getAddress() == null){
            throw new BusinessException("店铺地址不能为空");
        }

        //百度AI智能认证
        if( authService.authentication(shop.getName())){
            throw new BusinessException("店铺名存在违规信息");
        }


        //校验店铺是否存在
        ShopQuery shopQuery = new ShopQuery();
        shopQuery.setName(shop.getName());
        Long count = shopMapper.findByQueryCount(shopQuery);
        if(count != 0){
            throw new BusinessException("店铺名["+shop.getName()+"]已被注册");
        }
        //补充默认数据
        shop.setRegisterTime(new Date());//注册时间
        shop.setState(BaseConstants.Shop.STATE_WAIT_AUTID);//审核状态

        //获取到注册的管理员的信息数据
        Employee admin = shop.getAdmin();
        //保存数据库 得到admin_id
        employeeMapper.add(admin);

        shop.setAdminId(admin.getId());//将admin的id设置为shop的admin_id

        shopMapper.add(shop);//保存数据库 得到店铺的id

        admin.setShop_id(shop.getId());//将shop的id设置为admin的shop_id
        employeeMapper.update(admin);//执行修改操作保存进去
    }

    @Override
    public void update(Shop shop) {
        super.update(shop);
    }

    @Override
    public List<ShopRo> conutByDate() {
        return shopMapper.conutByDate();
    }

    @Override
    public List<ShopEx> countByTime() {
        return shopMapper.countByTime();
    }

    @Override
    public void activation(Long shopId) {
        Shop one = shopMapper.findOne(shopId);
        one.setState(BaseConstants.Shop.STATE_ACTIVE_SUCCESS);
        shopMapper.update(one);
        //得到管理员id
        Long adminId = one.getAdminId();
        Employee admin = employeeMapper.findOne(adminId);
        admin.setState(BaseConstants.Employee.STATE_NORMAL);
        employeeMapper.update(admin);
    }
}
